%%
% FWA main script
addpath(genpath('./../benchmark/'));
% format long
clc;
clear;
params.seeds = 5;
params.dimension = 10;
params.upper_bound = 20;
params.lower_bound = -20;
params.iterations = 400;
params.coef_explosion_sparks = 50;
params.coef_explosion_a = 0.04;
params.coef_explosion_b = 0.8;
params.coef_amplitude = 40;
params.mutation_number = 5;
params.scale_factor = 0.4;
params.crossover_rate = 0.2;
params.func = @Sphere;

% test calculate sparks function
global coef_explosion_sparks;
global coef_explosion_a;
global coef_explosion_b;

coef_explosion_sparks = params.coef_explosion_sparks;
coef_explosion_a = params.coef_explosion_a;
coef_explosion_b = params.coef_explosion_b;

seeds_matrix = rand(params.seeds, params.dimension) * (params.upper_bound - ...
                                    params.lower_bound) + params.lower_bound;
for i = 1:params.seeds
    seeds_fitness(i) = params.func(seeds_matrix(i,:));
end

sparks_number = calculate_sparks(seeds_fitness, 1);
%%
% [seeds_distance_total, distance_total] = calculate_seeds_distance(seeds_matrix);
%%
% test calculate amplitude
global coef_amplitude;

coef_amplitude = params.coef_amplitude;

amplitude_of_ith = calculate_amplitude(seeds_fitness, 1);

%%
% test generate new sparks
global dimension;
global upper_bound;
global lower_bound;

dimension = params.dimension;
upper_bound = params.upper_bound;
lower_bound = params.lower_bound;

ith_sons_sparks_matrix = generate_new_sparks(seeds_matrix(1,:), sparks_number, amplitude_of_ith);

%%
% test gaussian mutation
ith_seed_mutation = gaussian_mutation(seeds_matrix(1,:));

% test probability
probability_of_seed = probability(seeds_matrix(1,:), seeds_matrix);

% test select next generation
global seeds;
global func;

seeds = params.seeds;
func = params.func;
all_seeds_matrix = [ith_sons_sparks_matrix; ith_seed_mutation; seeds_matrix];
% all_seeds_matrix = seeds_matrix;
next_generation_matrix = select_next_generation(all_seeds_matrix);
%%
global scale_factor;
global crossover_rate;
scale_factor = params.scale_factor;
crossover_rate = params.crossover_rate;

% test differential operation
next_seeds_matrix = differential_operation(seeds_matrix');